package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.constant.UserConstant;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WeMediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private WeMediaFeign weMediaFeign;

    @Autowired
    private ArticleFeign articleFeign;

    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {

        if (null == dto) {
            return ResponseResult.errorResult (AppHttpCodeEnum.PARAM_INVALID);
        }

        dto.checkParam ();

        Page<ApUserRealname> page = new Page<> (dto.getPage (), dto.getSize ());

        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<> ();

        if (null != dto.getStatus ()) {
            queryWrapper.like (ApUserRealname::getStatus, dto.getStatus ());
        }

        IPage<ApUserRealname> iPage = page (page, queryWrapper);

        PageResponseResult result = new PageResponseResult (dto.getPage (), dto.getSize (), (int) iPage.getTotal ());

        result.setData (iPage.getRecords ());

        return result;
    }

    @Override
    @GlobalTransactional
    @Transactional
    public ResponseResult authPass(AuthDto dto, short status) {
        if (null == dto) {
            return ResponseResult.errorResult (AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUserRealname apUserRealname = new ApUserRealname ();
        apUserRealname.setId (dto.getId ());
        apUserRealname.setStatus (status);
        if (StringUtils.isNotBlank (dto.getMsg ())) {
            apUserRealname.setReason (dto.getMsg ());
        }
        boolean updateResult = this.updateById (apUserRealname);
        if (!updateResult) {
            return ResponseResult.errorResult (AppHttpCodeEnum.UPDATE_FAIL, "审核失败");
        }
        if (status == UserConstant.AUTH_PASS) {
            ApUserRealname userRealname = this.getById (dto.getId ());
            ApUser apUser = apUserMapper.selectById (userRealname.getUserId ());
            WmUser wmUser = weMediaFeign.queryWmUserByName (apUser.getName ());
            if (wmUser == null) {

                wmUser = new WmUser ();
                wmUser.setApAuthorId (apUser.getId ());
                wmUser.setName (apUser.getName ());
                wmUser.setPassword (apUser.getPassword ());
                wmUser.setSalt (apUser.getSalt ());
                wmUser.setPhone (apUser.getPhone ());
                wmUser.setStatus (9);
                wmUser.setCreatedTime (new Date ());

                weMediaFeign.saveWmUser (wmUser);
            }
            apUser.setFlag ((short) 1);
            apUserMapper.updateById (apUser);

            ApAuthor apAuthor = articleFeign.findByUserId (apUser.getId ());
            if (null == apAuthor) {
                apAuthor = new ApAuthor ();
                apAuthor.setName (apUser.getName ());
                apAuthor.setType (2);
                apAuthor.setUserId (apUser.getId ());
                apAuthor.setCreatedTime (new Date ());
                articleFeign.save (apAuthor);
            }

        }
        return ResponseResult.okResult (AppHttpCodeEnum.SUCCESS);
    }


}
